slimecing

a fighting game featuring slimes and swords
Log | Files | Refs | README

SimpleController_UsingState.cs (2550B)


      1 using UnityEngine.InputSystem;
      2 using UnityEngine;
      3 
      4 // Using state of gamepad device directly.
      5 public class SimpleController_UsingState : MonoBehaviour
      6 {
      7     public float moveSpeed;
      8     public float rotateSpeed;
      9     public GameObject projectile;
     10 
     11     private Vector2 m_Rotation;
     12     private bool m_Firing;
     13     private float m_FireCooldown;
     14 
     15     public void Update()
     16     {
     17         var gamepad = Gamepad.current;
     18         if (gamepad == null)
     19             return;
     20 
     21         var leftStick = gamepad.leftStick.ReadValue();
     22         var rightStick = gamepad.rightStick.ReadValue();
     23 
     24         Look(rightStick);
     25         Move(leftStick);
     26 
     27         if (gamepad.buttonSouth.wasPressedThisFrame)
     28         {
     29             m_Firing = true;
     30             m_FireCooldown = 0;
     31         }
     32         else if (gamepad.buttonSouth.wasReleasedThisFrame)
     33         {
     34             m_Firing = false;
     35         }
     36 
     37         if (m_Firing && m_FireCooldown < Time.time)
     38         {
     39             Fire();
     40             m_FireCooldown = Time.time + 0.1f;
     41         }
     42     }
     43 
     44     private void Move(Vector2 direction)
     45     {
     46         if (direction.sqrMagnitude < 0.01)
     47             return;
     48         var scaledMoveSpeed = moveSpeed * Time.deltaTime;
     49         // For simplicity's sake, we just keep movement in a single plane here. Rotate
     50         // direction according to world Y rotation of player.
     51         var move = Quaternion.Euler(0, transform.eulerAngles.y, 0) * new Vector3(direction.x, 0, direction.y);
     52         transform.position += move * scaledMoveSpeed;
     53     }
     54 
     55     private void Look(Vector2 rotate)
     56     {
     57         if (rotate.sqrMagnitude < 0.01)
     58             return;
     59         var scaledRotateSpeed = rotateSpeed * Time.deltaTime;
     60         m_Rotation.y += rotate.x * scaledRotateSpeed;
     61         m_Rotation.x = Mathf.Clamp(m_Rotation.x - rotate.y * scaledRotateSpeed, -89, 89);
     62         transform.localEulerAngles = m_Rotation;
     63     }
     64 
     65     private void Fire()
     66     {
     67         var transform = this.transform;
     68         var newProjectile = Instantiate(projectile);
     69         newProjectile.transform.position = transform.position + transform.forward * 0.6f;
     70         newProjectile.transform.rotation = transform.rotation;
     71         const int size = 1;
     72         newProjectile.transform.localScale *= size;
     73         newProjectile.GetComponent<Rigidbody>().mass = Mathf.Pow(size, 3);
     74         newProjectile.GetComponent<Rigidbody>().AddForce(transform.forward * 20f, ForceMode.Impulse);
     75         newProjectile.GetComponent<MeshRenderer>().material.color =
     76             new Color(Random.value, Random.value, Random.value, 1.0f);
     77     }
     78 }